<?php

namespace logInController;

require_once '\view\LogInPage.php';
require_once '\view\Bootstraphtml.php';
require_once '\model\LogInModel.php';

class LogInController{
	
	private $model;
	private $viewlogInPage;
	private $viewboothtml;
	private $LoggedInStatus;
		
	public function __construct(){
		$this->model = new \logInModel\LogInModel();
		$this->viewlogInPage = new \logInPage\LogInPage();
		$this->viewboothtml = new \bootstraphtml\Bootstraphtml();
	}		
		
	public function control(){
		
		$this->checkButtons();
		$body = $this->getBody();
		
	
		$html = $this->viewboothtml->getbootstraphtml($body);
		return $html;
	}
	private function getBody(){
		$message = $this->model->getMessage();
		$this->viewlogInPage->setMessage($message);
		
		if($this->model->checkIfLoggedIn() == TRUE){
			$usernameSession = $this->model->getUsername();
            $header = $this->viewlogInPage->loggedInHeader($usernameSession);
			$page = $this->viewlogInPage->getLoggedInPage();
		}else{
			$header = $this->viewlogInPage->loggedOutHeader();
			$page = $this->viewlogInPage->getLoggedOutPage();
		}
		
		$body = $header . $page; 
		
		return $body;
	}
	private function checkButtons(){
		if($this->viewlogInPage->registrerButton() == TRUE){
			$this->doRegistrer();
		}else if($this->viewlogInPage->logInButton() == TRUE){
			$this->loginControlChecker();
		}else if ($this->viewlogInPage->logOutButton() == TRUE){
			$this->model->logOut();
		}
	}
	private function loginControlChecker(){
		$username = $this->viewlogInPage->getUsername();
		$password = $this->viewlogInPage->getPassword();
		$this->LoggedInStatus = $this->model->tryLogIn($username, $password);
	}
	private function doRegistrer(){
			$username = $this->viewlogInPage->getUsername();
			$password = $this->viewlogInPage->getPassword();
			$this->model->addUser($username, $password);	
	}
}
